public class SimplePlannerAI implements AI {
  public Action decide(Battle game_state) {
    Action best_action = null;
    double best_priority = 0.0;

    for(Action a: game_state.successors()) {
      double priority = 0.0;
      for(Action.Outcome outcome: a.branch_out(game_state))
        priority += value(outcome.battle) * outcome.probability;
      if(best_action == null || priority > best_priority) {
        best_action = a;
        best_priority = priority;
        Main.log.println("Considering "+a);
      }
    }

    if(best_action == null)
      throw new Error("Assertion failed! successors() returned <empty>");
    Main.log.println("Chose: "+best_action);
    return best_action;
  }

  private double value(Battle game_state) {
    double result = 0.0;
    for(Combatant c: game_state.roster(Team.RED))
      result += Math.sqrt(c.hp()) * 10.0;
    for(Combatant enemy: game_state.roster(Team.BLUE)) {
      result -= Math.sqrt(enemy.hp()) * 20.0;
      for(Combatant c: game_state.roster(Team.RED))
        result -= game_state.distance(game_state.loc(c),game_state.loc(enemy));
    }
    return result;
  }
}

